﻿/*
 * Created by: 
 * Created: Friday, April 20, 2007
 */

using System;
using System.Runtime.Remoting.Lifetime;
using System.Web.UI;
using Dune.Common.TextTemplates.Impl.ASPNET;

namespace Dune.Common.TextTemplates.Impl.ASPNET
{
    public class AppHost : MarshalByRefObject, IAppHost, IDisposable
    {
        private Guid id = Guid.NewGuid();
        private string dataKey;

        public AppHost()
        {
            dataKey = "Dune.Common.TextTemplates.Impl.ASPNET.AppHost" + id.ToString();
            // create ref to object in doamin context
            AppDomain.CurrentDomain.SetData(dataKey, this);
        }

        public TypeLocationInfo GetLocationInfo(string controlName)
        {
            UserControl uc = new UserControl();
            Control ret = uc.LoadControl(controlName);
            return new TypeLocationInfo(ret.GetType().FullName, ret.GetType().Assembly.CodeBase);
        }


        public override object InitializeLifetimeService()
        {
            // this code from msdn sample
            //            ILease lease = (ILease)base.InitializeLifetimeService();
            //            if (lease.CurrentState == LeaseState.Initial)
            //            {
            //                lease.InitialLeaseTime = TimeSpan.FromMinutes(1);
            //                lease.SponsorshipTimeout = TimeSpan.FromMinutes(2);
            //                lease.RenewOnCallTime = TimeSpan.FromSeconds(2);
            //            }
            //            return lease;

            // http://msdn.microsoft.com/msdnmag/issues/03/12/leasemanager/
            // When you design a singleton object, override InitializeLifetimeService and return a null object as 
            // the new lease, indicating that this lease never expires.
            return null;

        }

        #region IDisposable Members

        ///<summary>
        ///Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        ///</summary>
        ///<filterpriority>2</filterpriority>
        public void Dispose()
        {
            // break here
        }

        #endregion
    }
}